/**
 * Created by Administrator on 2017/7/20.
 */
(function() {
    "use strict";
    var app = angular.module("angularApp");
    app.controller('attachmentCtrl', function ($scope,$rootScope, $http,$uibModal,fac,$timeout) {
        $scope.search = {};
      /*  $(document).on("resize","#iconScroll",function(){
            $(this).css("height",window.height-180);
        })*/
       /* $scope.find = function(pageNo){
            if($scope.curType){
                $scope.search.typeId = $scope.curType.id;
                $.extend($scope.search,{currentPage:pageNo||$scope.curType.pageModel.currentPage||1,pageSize:$scope.curType.pageModel.pageSize||20000});
                fac.getPageResult("../icon/list",$scope.search,function(data){
                    $scope.curType.pageModel = data;
                    $("#iconScroll").css("height",window.innerHeight-190);
                });
            }
        };*/

         $scope.find  = function(pageNo){
            if($scope.curType){
                $scope.search.folderId = $scope.curType.id;
                $.extend($scope.search,{currentPage:pageNo||$scope.pageModel.currentPage||1,pageSize:$scope.pageModel.pageSize||10});
                fac.getPageResult("../attachment/list",$scope.search,function(data){
                    $scope.pageModel = data;
                });
            }
        };

        $http.get("../attachment/listFolders").success(function(resp){
            if(resp.code === 0){
                $scope.typeList = resp.data;
                $scope.typeList.length && ($scope.selectType($scope.typeList[0]));
                $("#folderScroll").css("height",window.innerHeight-90);
            }else{
                alert(resp.msg);
            }
        })
        // icons.unshift(0,0);
       //  picList.splice.apply(picList,icons);
        $scope.addFiles = function (params) {
            fac.upload({ url: "/attachment/uploadFile" ,multiple:true,params:params,accept:"text/plain" }, function (resp) {
                if (resp.code == 0 ) {
                    var files = resp.data.files;
                    var  deleteIds = resp.data.deleteIds;
                    var message = "成功上传"+files.length+"个附件！";
                    if(deleteIds && deleteIds.length){
                        message +="共"+deleteIds.length+"个附件被替换！"
                    }
                    files.forEach(function(file){
                        var toDel = $scope.pageModel.data.find(function(m){return m.name == file.name});
                        if(toDel){
                            $scope.pageModel.data.splice($scope.pageModel.data.indexOf(toDel),1);
                            $scope.pageModel.totalRecord--;
                        }
                        $scope.pageModel.data.splice(0,0,file);
                        $scope.pageModel.totalRecord ++;
                    })
                    msg(message)
                    $scope.$apply();
                } else {
                    alert(resp.msg);
                }
            })
        }
        $scope.delAll = function(){
            var ids = $scope.pageModel.list.reduce(function (ret, n) {
                n.checked && ret.push(n.id);
                return ret
            }, []);
            confirm("确认删除选中的 " + ids.length + " 个文件吗?",function(){
                $http.post("../attachment/del", {
                    "ids": ids.join()
                }, fac.postConfig).success(function (resp) {
                    if (resp.code == 0) {
                        ids.forEach(function(id){
                            var toDel = $scope.pageModel.list.find(function(m){return m.id == id});
                            if(toDel){
                                $scope.pageModel.list.splice($scope.pageModel.list.indexOf(toDel),1);
                                $scope.pageModel.totalRecord --;
                            }
                        })
                    } else {
                        alert(resp.msg);
                    }
                })
            })
        }
        $scope.del = function(item){
            confirm("确认删除该附件吗？",function(){
                $http.get("../attachment/del?ids="+item.id).success(function(resp){
                    if(resp.code === 0){
                        $scope.find();
                    }else{
                        alert(resp.msg);
                    }
                })
            });
        }

        $scope.downloadZip = function(){
            var ids = $scope.pageModel.list.reduce(function (ret, n) {
                n.checked && ret.push(n.id);
                return ret
            }, []);
            confirm("确认打包下载选中的 " + ids.length + " 个附件吗?",function(){
                try{
                    var elemIF = document.createElement("iframe");
                    elemIF.src = "/attachment/downloadZip?ids="+ids.join();
                    elemIF.style.display = "none";
                    document.body.appendChild(elemIF);
                }catch(e){

                }
            })
        }
        $scope.editAll = function(){
            var items = $scope.iconTypes[0].pageModel.list.filter(function (n) {n.checked&&(n.newName = n.name);return n.checked;});
            items.push.apply(items,$scope.iconTypes[1].pageModel.list.filter(function (n) {n.checked&&(n.newName = n.name);return n.checked;}));
            var modal = $uibModal.open({
                animation: false,
                size:'lg',
                templateUrl: 'view/icon/icon.modal.html',
                controller: 'iconEditCtrl'
                ,resolve: {items: function(){return items;}}
            });
            modal.result.then(function () {

            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        }

        $scope.selectType = function(type){
           var curItem = $scope.typeList.find(function(n){return n.active});
           if(curItem && curItem!=type){
               curItem.active = false;
           }
           type.active = true;
           $scope.pageModel =$scope.pageModel||{};
           $scope.curType = type;
           $scope.find(1);
        }

        $scope.editType = function(type){
            type.textCopy = type.text;
            type.edit = true;
            var input = $(event.target).parents("li").children("input");
            $timeout(function(){
                input.select();
            })
        }
        $scope.delType = function(type){
            $http.get("../attachment/existFile?folderId="+type.id).success(function(resp){
                if(resp.code ==0){
                    if(resp.data ==1){
                        alert(type.text+"文件夹下存在文件，请清空后再删除！");
                    }else{
                        confirm("确认删除分类"+type.text+"？",function(){
                            if($scope.curType == type){
                                delete $scope.curType;
                            }
                            $http.get("../attachment/delFolder?folderId="+type.id).success(function(resp){
                                if(resp.code === 0){
                                    $scope.typeList.splice($scope.typeList.indexOf(type),1);
                                }else{
                                    alert(resp.msg);
                                }
                            })
                        });
                    }
                }
            })
        }
        $scope.saveType = function(form,type){
            form.$setSubmitted(true);
            if (!form.$valid) {
                return;
            }
            var copy = {};
            angular.extend(copy,type);
            copy.text = type.textCopy;
            $http.post("../attachment/saveFolder",copy).success(function(resp){
                if(resp.code === 0){
                    type.edit = false;
                    type.id = resp.data.id;
                    type.text = resp.data.text;
                }else{
                    alert(resp.msg);
                }
            })
        }
        $scope.cancelEdit = function(type){
            if(type.id){
                type.edit = false;
            }else{
                $scope.typeList.splice($scope.typeList.indexOf(type),1);
            }
        }
    });

})();